﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class AdminUserGateway:DBGateway
    {
        public string SaveNewUser(AdminUser adminUser)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblAdminUser values(@Name,@Password,@Emil,@Post)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@Name", SqlDbType.VarChar).Value = adminUser.Name;
                CommandObj.Parameters.Add("@Password", SqlDbType.VarChar).Value = adminUser.Password;
                CommandObj.Parameters.Add("@Emil", SqlDbType.VarChar).Value = adminUser.Email;
                CommandObj.Parameters.Add("@Post", SqlDbType.Bit).Value = adminUser.Post;
                CommandObj.ExecuteNonQuery();
                return "Admin user save successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }
        public string UpdateUser(AdminUser adminUser)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Update tblAdminUser Set Name='{0}',Password='{1}'Email='{2}',where Id='{3}'",adminUser.Name,adminUser.Password,adminUser.Email,adminUser.Id);
                CommandObj.ExecuteNonQuery();
                return "Admin user update successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }
        public bool HasThisAdminUserExist(AdminUser adminUser)
        {
            try
            {
                ConnectionObj.Open();
                string select = String.Format("Select * From tblAdminUser Where UserEmail='{0}'",adminUser.Email);
                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Adminuser checking failed, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }

        public bool HasThisEmailPass(AdminUser adminUser)
        {
            try
            {
                ConnectionObj.Open();
                string select = String.Format("Select * From tblAdminUser Where UserEmail='{0}' and Password='{1}'",adminUser.Email,adminUser.Password);
                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Password does not change, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }
    }
}